package wjl.project.dubboelabauthconsumer.SecurityConfig;

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import wjl.project.dubboelabauthapi.Consts;
import wjl.project.dubboelabauthapi.RedisKeys;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class SecurityProvider implements AuthenticationProvider {

    @Resource
    SecurityUserDetailService securityUserDetailService;

    @Resource
    RedissonClient redissonClient;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 获取表单输入中返回的用户名
        String phone = (String) authentication.getPrincipal();
        // 获取表单中输入的密码
        String password = (String) authentication.getCredentials();
        CustomWebAuthenticationDetails customWebAuthenticationDetails = (CustomWebAuthenticationDetails) authentication.getDetails();
        String method = customWebAuthenticationDetails.getLoginMethod();
        // 查询用户是否存在
        SecurityUser userInfo = (SecurityUser) securityUserDetailService.loadUserByUsername(phone);
        if (userInfo == null) {
            throw new UsernameNotFoundException("手机号不存在");
        }
//        手机验证登录
        if (method.equals(Consts.LOGIN_METHOD_PHONE)){
            RBucket<Integer>  rBucket =redissonClient.getBucket(RedisKeys.PHONE_KEY.getKey()+phone);
            if (rBucket==null){
                throw new BadCredentialsException("验证码未发送");
            }
            String code = String.valueOf(rBucket.get());

            if (!code.equals(password))
            {
                throw new BadCredentialsException("验证码不正确");
            }
        }
//        普通验证登录
        if (method.equals(Consts.LOGIN_METHOD_NORMAL)){
            if (!new BCryptPasswordEncoder().matches(password, userInfo.getPassword())) {
                throw new BadCredentialsException("密码不正确");
            }
        }

//        验证成功
        password = userInfo.getPassword();
        // 进行登录
        return new UsernamePasswordAuthenticationToken(userInfo, password,null);

    }

    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }
}
